import processing.pdf.*;
Table t;
TreeMap tm;
int rowCount, labelCount;
int count, col, column, time;
float percent;
String title, title2;
PFont fontA, fontB;


void setup(){
  //size(200, 200, PDF, "out.pdf"); 
  // t = new Table("Reclass-08-09.tsv");
  // t = new Table("Equities-08-09.tsv");
  t = new Table("wf-08-09.tsv");
  // t = new Table("EMP-apps-08-09.tsv");
  // t = new Table("EMP-Hires-08-09.tsv");
  // t = new Table("EMP-salary-placement.tsv");
  // t = new Table("Separations-08-09.tsv");
  // t = new Table("EMP-Time-To-Fill.tsv");
  size(480, 620);  
  //fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
  fontA = loadFont("Candara-Bold-32.vlw");
  fontB = loadFont("BerlinSansFB-Reg-18.vlw");   
}

void draw(){
  tm = new TreeMap();
  crunchData(column); 
  // THis is green fill(#577737);
  fill(255);
  rect(0,0,width,height);
  displayText(tm);
}

void displayText(TreeMap hm){
  fill(25,175);
  textFont(fontA, 32);
  text(t.getString(0,column),15,40);
  Iterator i = hm.entrySet().iterator();  // Get an iterator
  int s=0;
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    s+=17;
    textFont(fontB, 18);
    text(f+"% \t", width-100,40+s);
    text(me.getKey()+" ["+me.getValue()+"]",width-450,40+s);
  }
  textFont(fontA, 32);
  text(((rowCount-1)+" Total lines"),width-290,height-15); // subtract one for the title column
}

void crunchData(int col){
  rowCount = t.getRowCount(  );
  title = t.getString(0, col);

  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, col);

    if(tm.containsKey(value)){
      count = (Integer)tm.get(value);
      tm.put(value,++count);
    }
    else if(!tm.containsKey(value)){
      tm.put(value,1);
    }     
  }  
}

void crunchData(int col, int col2){
  rowCount = t.getRowCount(  );
  title = t.getString(0, col);
  title2 = t.getString(0,col2);

  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, col);

    if(tm.containsKey(value)){
      time = (Integer)tm.get(value);
      tm.put(value,++time);
    }
    else if(!tm.containsKey(value)){
      time = (Integer)tm.get(value);
      tm.put(value,time);
    }     
  }  
}

void keyPressed() {
  if (key == '[') {
    column--;
    if (column < 0) {
      column = t.getRowLength() - 1;
    }
  } 
  else if (key == ']') {
    column++;
    if (column == t.getRowLength()) {
      column = 0;
    }
  }
  else if (key == 'p'){
    printMapContents(tm);
  }
  else if (key == 'c'){
    println();
    println();
    println();
  }  
}

void printMapContents(TreeMap tm){
  println(t.getString(0,column));
  Iterator i = tm.entrySet().iterator();  // Get an iterator
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    print(f+"% \t");
    print(me.getKey()+" ["+me.getValue()+"]");
    println();
  }
  println(((rowCount-1)+" Total lines")); // subtract one for the title column
}
/* 
 Based off of some great example code from Quark
 http://processing.org/discourse/yabb2/YaBB.pl?num=1251333472/9 
 
 treeMap example
 http://www.java2s.com/Code/Java/Collections-Data-Structure/SortingElementsinaTreeMap.htm
 */


